/****************************************************************************
 *
 * Copyright (C) 2003-2010 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.firewall;

import java.util.StringTokenizer;

import nfse.NetFSE;
import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.record.FlexRecordField;
import nfse.gui.server.GUIRecordFactory;
import nfse.gui.server.RecordBuilderUtil;

public class IPFWRecordFactory extends GUIRecordFactory<IPFWRecord> {

    public IPFWRecordFactory() {
        builder = new RecordBuilderUtil();
        builder.addColumn(null,"ID", RecordBuilderUtil.BIGINT);
        RecordBuilderUtil.SQLField field = builder.addColumn("Start TS","startts", RecordBuilderUtil.TIMESTAMP);
        field = builder.addColumn("Src IP","srcip", RecordBuilderUtil.INET);
        field = builder.addColumn("Src Port","srcport", RecordBuilderUtil.INTEGER);
        field.setSubtype(FlexRecordField.SUB_TYPE_PORT);
        field = builder.addColumn("Dst IP","dstip", RecordBuilderUtil.INET);
        field = builder.addColumn("Dst Port","dstport", RecordBuilderUtil.INTEGER);
        field.setSubtype(FlexRecordField.SUB_TYPE_PORT);
        field = builder.addColumn("Protocol","protocol", RecordBuilderUtil.INTEGER);
        field.setSubtype(FlexRecordField.SUB_TYPE_PROTOCOL);
        field = builder.addColumn("Action","action", RecordBuilderUtil.VARCHAR,10);
        field = builder.addColumn("Direction","direction", RecordBuilderUtil.VARCHAR,5);
        field = builder.addColumn("Interface","interfacename", RecordBuilderUtil.VARCHAR,15);
    }
    
    @Override
    protected void setValues(IPFWRecord record, int id) {
        int idx = -1;
        builder.setFieldValue(++idx, "" + id);
        builder.setFieldValue(++idx, "" + record.getStartTS());
        builder.setFieldValue(++idx, record.getSrcAddr());
        builder.setFieldValue(++idx, "" + record.srcPort);
        builder.setFieldValue(++idx, record.dstAddr);
        builder.setFieldValue(++idx, "" + record.dstPort);
        builder.setFieldValue(++idx, "" + record.proto);
        builder.setFieldValue(++idx, record.action );
        builder.setFieldValue(++idx, record.direction);
        builder.setFieldValue(++idx, record.interfaceName);
    }
    
    public int[] getIndexedFields() {
        int[] temp = { NetFSE.FIELD_START_TS, NetFSE.FIELD_SRC_IP, NetFSE.FIELD_DST_IP, NetFSE.FIELD_DST_PORT };
        return temp;
    }
    
    @Override
    public NetFSERecord generate(String line, int sensor) {
        try {
            // Record of the form: Jan 17 13:48:08 benjamin-uphoffs-computer
            // tag: 12190 Deny TCP 10.0.1.100:54765 10.0.1.195:2 in via en1
            IPFWRecord rec = new IPFWRecord(sensor);
            // int idx1 = line.indexOf(";");
            // int idx2 = line.indexOf(";", idx1 + 2);

            line = line.trim();

            StringTokenizer st = new StringTokenizer(line);

            rec.setInitialString(line);
            rec.setStartTS(NetFSEUtil.timestampFromSyslog(line));

            String token = st.nextToken();
            while (!token.endsWith(":")) { // Advance to the end of the header
                token = st.nextToken();
            }

            st.nextToken();
            rec.action = st.nextToken();
            rec.proto = NetFSEUtil.getProtocol(st.nextToken());

            token = st.nextToken(); // the source info
            int idx = token.indexOf(":");
            rec.setSrcIP(NetFSEUtil.convertIPtoLong(token.substring(0, idx)));
            rec.srcPort = Integer.parseInt(token.substring(idx + 1));

            token = st.nextToken(); // the destination info
            idx = token.indexOf(":");
            rec.setDstIP(NetFSEUtil.convertIPtoLong(token.substring(0, idx)));
            rec.dstPort = Integer.parseInt(token.substring(idx + 1));

            rec.direction = st.nextToken();
            st.nextToken();
            rec.interfaceName = st.nextToken();

            return rec;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null;
    }
    
    @Override
    public NetFSERecord generate() {
        return new IPFWRecord();
    }

}
